﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.Business
{
    public partial class MeterReadingPlan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMeterPlan();
            }
        }

        protected void BindMeterPlan()
        {
            string yy = ddrYear.SelectedValue;
            string url = WebapiUrl.weburl() + "GetMeterPlans/" + yy;

            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url)
                .Result.Content.ReadAsStringAsync().Result;
            IList<MeterPlanQuery> mr = JsonConvert.DeserializeObject<IList<MeterPlanQuery>>(responseJson);
            if (mr == null)
            {
                lbmessage.Text = "没有找到相关记录！请重新输入查询条件！";
                return;
            }
            Gvmeterplan.DataSource = mr;
            Gvmeterplan.DataBind();
        }

        protected void ddrYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMeterPlan();
        }
        protected void BuilderPlan_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "AddMeterPlan";
            MeterPlan mp = new MeterPlan()
            {
                MeterMM = ddrYear.SelectedValue,
                MeterDay = tbday.Text,
                MeterEmp = "",
                AccountNo = "",
                WaterNo = "",
                IsComplete = "F", 
                Memo = "",
                CreateEmp = Membership.GetUser().UserName,
                CreateDt = DateTime.Now 
            };
            var requestJson = JsonConvert.SerializeObject(mp);
            HttpContent httpContent = new StringContent(requestJson);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync(url, httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            if (responseJson == "true")
            {
                BindMeterPlan();
            }
        }
    }
}